<template>
    <div style="padding:20px">
        <div style="display: flex;justify-content: space-between;align-items: center">
            <h1 class="title">设备列表</h1>
            <el-button type="primary" @click="drawerVisible = true"><el-icon>
                    <Plus />
                </el-icon>创建设备</el-button>
        </div>
        <div>
            <div v-if="deviceList?.length == 0">
                <el-empty description="暂无设备"></el-empty>
            </div>
            <el-row :gutter="12" v-else>
                <el-col :span="6" v-for="device in deviceList">
                    <div style="margin:4px;background-color: #fff;">
                        <div>
                            <img src="../../assets/deviceBanner.webp" style="width: 100%;height: auto;" />
                        </div>
                        <div style="padding:8px">
                            <div>{{ device.name }} ，来自于{{ device.location }}</div>
                            <div></div>
                            <!-- <div>来源：</div> -->
                            <div style="color:#999"><small>最后使用时间：{{ device.login_time }}</small></div>
                            <div style="color:#999"><small>ID：{{ device.device_id }}</small></div>
                            <div style="text-align: right;">
                                <el-button size="small" type="primary"
                                    @click="() => { this.$router.push(`/deviceInfo/${device.id}`) }">查看</el-button>
                                <el-button size="small" type="danger" @click="deleteDevice(device)">删除</el-button>
                            </div>
                        </div>
                    </div>
                </el-col>
            </el-row>
            <!-- </div> -->
            <!-- <el-table :data="deviceList">
                <template #empty>
                    <el-empty description="暂无设备，快去创建吧~" />
                </template>
<el-table-column prop="id" label="ID" />
<el-table-column prop="name" label="设备名称" />
<el-table-column prop="device_id" label="设备device_id" />
<el-table-column prop="introduction" label="设备说明" />
<el-table-column prop="location" label="设备所在地" />
<el-table-column prop="create_time" label="注册时间" />
<el-table-column prop="login_time" label="最后一次使用时间" />
<el-table-column fixed="right" label="操作" min-width="120">
    <template #default="scope">
                        <el-button link type="primary" size="small">
                            详情
                        </el-button>
                        <el-button link type="primary" size="small">删除</el-button>
                    </template>
</el-table-column>
</el-table> -->
        </div>
    </div>

    <el-drawer v-model="drawerVisible" title="新增设备" :direction="direction" :before-close="handleClose">
        <el-form label-position="top">
            <el-form-item label="设备名称">
                <el-input placeholder="请输入设备名称。" v-model="form.name" />
            </el-form-item>
            <el-form-item label="设备介绍">
                <el-input placeholder="请输入设备介绍。" v-model="form.introduction" />
            </el-form-item>
            <el-form-item label="设备Id">
                <el-input placeholder="请输入设备device_id，位于设备说明书的Id标签中。" v-model="form.device_id" />
            </el-form-item>
        </el-form>

        <template #footer>
            <div style="flex: auto">
                <el-button @click="drawerVisible = false">取消</el-button>
                <el-button type="primary" @click="createDevice">确认</el-button>
            </div>
        </template>
    </el-drawer>
</template>
<script>
import { fa } from 'element-plus/es/locale/index.mjs';
import { POST } from '../../util/axios.js'
export default {
    data() {
        return {
            deviceList: [],
            drawerVisible: false,
            form: { device_id: "", name: "", introduction: "", location: "", id: 0 },
        }
    },
    methods: {
        async getDeviceList() {
            let rs = await POST("/deviceBank/getDeviceList", "POST", {}, false)
            console.log("rs:>>:", rs)
            this.deviceList = rs.data
        },
        async createDevice() {
            let rs = await POST("/deviceBank/createDevice", "POST", this.form, true)
            console.log("rs:>>:", rs)
            this.drawerVisible = false
            this.getDeviceList()
            // this.deviceList = rs.data
        },
        async deleteDevice(row) {
            let rs = await POST("/deviceBank/deleteDevice", "POST", row, true)
            console.log("rs:>>:", rs)
            this.getDeviceList()
        }
    },
    mounted() {
        this.getDeviceList()
    }
}
</script>
<style></style>